#include "BondDetailPrcRelevance.h"
#include "BondDetailPrcRelevanceFilter.h"
#include "BondDetailData.h"
#include "BondDetailTools.h"
#include <core/StyleMgr.h>
#include <QTabWidget>
#include <QStylePainter>
#include <uam/UserAccountManager.h>
#include <QVBoxLayout>

BondDetailPrcRelevance::BondDetailPrcRelevance(QWidget* p, QSharedPointer<DetailData> data)
	: QWidget(p), m_data(data),m_nCurrentTab(RELEVANCEFILTERISSUER)
{
	setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Expanding);

	m_tabWidget = new QTabWidget();
	QString m_styleSheet = QString(
		"QTabWidget::tab-bar		{left:1px;}"
		"QTabBar::tab				{font:bold;width:98px;height:20px;margin-right:2px;}"
		"QTabBar::tab:selected		{background-color:rgb(246,141,32);color:rgb(0,0,0);}"
		"QTabBar::tab:!selected		{background-color:rgb(24,68,69);color:rgb(255,255,255);}"
		"QTabWidget::pane			{border:1px solid #%1;}"
	)
	.arg(StyleMgr::instance().color("BorderNormal").rgb(), 0, 16);
	m_tabWidget->setStyleSheet(m_styleSheet);

	m_mapRecord.clear();

	QVBoxLayout* mainLayout = new QVBoxLayout;
	mainLayout->setContentsMargins(0, 0, 0, 0);
	mainLayout->addWidget(m_tabWidget);
	setLayout(mainLayout);
}

void BondDetailPrcRelevance::initCreditIdx()
{
	m_vec.clear();
	m_vec.push_back(emIdxNotice);
	m_vec.push_back(emIdxIssuer);
	m_vec.push_back(emIdxSecond);
	m_vec.push_back(emIdxPrimary);
	m_nTabCount = 4;
	m_nCurrentTab = 0;
}

void BondDetailPrcRelevance::initOthersIdx()
{
	m_vec.clear();
	m_vec.push_back(emIdxIssuer);
	m_vec.push_back(emIdxSecond);
	m_vec.push_back(emIdxPrimary);
	m_nTabCount = 3;
	m_nCurrentTab = 0;
}

void BondDetailPrcRelevance::initTab(int nTab)
{
	if (m_nCurrentTab < 0 || m_nCurrentTab >= m_nTabCount)	return;
	QString  strContent;
	switch (m_vec[nTab])
	{
		case emIdxNotice:
			loadTabWidget(m_vec[nTab]);
			if (!isLoaded("NOTICE"))
			{
				m_pNotice->refreshInfo(&m_bi);
				m_mapRecord.insert(std::make_pair(("NOTICE"), true));
			}
			break;
		case emIdxIssuer:
			loadTabWidget(m_vec[nTab]);
			if (!isLoaded("ISSUER"))
			{
				m_pIssuer->refreshData();
				m_mapRecord.insert(std::make_pair("ISSUER", true));
			}
			break;
		case emIdxSecond:
			loadTabWidget(m_vec[nTab]);
			if (!isLoaded("SECOND"))
			{
				m_pSecond->refreshData();
				m_mapRecord.insert(std::make_pair("SECOND", true));
			}
			break;
		case emIdxPrimary:
			loadTabWidget(m_vec[nTab]);
			if (!isLoaded("PRIMARY"))
			{
				m_pFirst->refreshData();
				m_mapRecord.insert(std::make_pair("PRIMARY", true));
			}
			break;
		default:break;
	}
}

void BondDetailPrcRelevance::refreshData()
{
	initTabIndx();

	for (int i = 0; i < m_nTabCount; i++)
		initTab(i);

	emit loadTabFinish();
}

void BondDetailPrcRelevance::loadTabWidget(int tabId)
{
	QString  strContent;
	switch (tabId)
	{
		case emIdxNotice:
		{
			if (!m_pNotice)
			{
				m_pNotice = new BondDetailNotice(this, m_data);
				strContent = tr("主承公告");
				m_tabWidget->addTab(m_pNotice, strContent);
			}
		}
		break;
		case emIdxIssuer:
		{
			if (!m_pIssuer)
			{
				m_pIssuer = new BondDetailPrcRelevanceFilter(this, m_data, RELEVANCEFILTERISSUER);
				strContent = tr("同发行人");
				m_tabWidget->addTab(m_pIssuer, strContent);
			}
		}
		break;
		case emIdxSecond:
		{
			if (!m_pSecond)
			{
				m_pSecond = new BondDetailPrcRelevanceFilter(this, m_data, RELEVANCEFILTERSECOND);
				strContent = tr("相似二级");
				m_tabWidget->addTab(m_pSecond, strContent);
			}
		}
		break;
		case emIdxPrimary:
		{
			if (!m_pFirst)
			{
				m_pFirst = new BondDetailPrcRelevanceFilter(this, m_data, RELEVANCEFILTERFIRST);
				strContent = tr("相似一级");
				m_tabWidget->addTab(m_pFirst, strContent);
			}
		}
		break;
		default:
			assert(false);
			break;
	}
}

bool BondDetailPrcRelevance::isLoaded(QString strKey)
{
	auto itrFind = m_mapRecord.find(strKey);
	return itrFind != m_mapRecord.end();
}

void BondDetailPrcRelevance::setBondInfo(QSharedPointer<DetailData> data)
{
	QB_CHECK_RETURN_VOID1(data);
	m_data = data;
	refreshData();
}

void BondDetailPrcRelevance::initTabIndx()
{
	if ( (strcmp(SSUserAccountManager::instance().GetPermission().GetValue(USER_PERMISSION_MUWNOTICE), "1") == 0) 
	  && (m_data->bond().is_credit_bond()))
		initCreditIdx();
	else
		initOthersIdx();
}
